package cs304.p3.library.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import cs304.p3.library.domain.HoldRequest;
import cs304.p3.library.util.SqlUtils;

public class HoldRequestDao extends Dao {
	public void insertHoldRequest(String hid, String bid, String callNumber, String issuedDate) throws SQLException {
		List<String> values = Lists.newArrayList();
		values.add(hid);
		values.add(bid);
		values.add(callNumber);
		values.add(issuedDate);
		String sql = SqlUtils.insertSql("HoldRequest", values);
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.executeUpdate();
	}
	
	public List<HoldRequest> selectHoldRequest(String callNumber) throws SQLException {
		Map<String,String> keyVals = Maps.newHashMap();
		keyVals.put("callNumber", callNumber);
		String sql = SqlUtils.selectSql("HoldRequest", keyVals);
		PreparedStatement ps = connection.prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		
		List<HoldRequest> holdRequests = Lists.newArrayList();
		while(rs.next()) {
			HoldRequest holdRequest = new HoldRequest();
			holdRequest.setHid(rs.getString("hid"));
			holdRequest.setBid(rs.getString("bid"));
			holdRequest.setCallNumber(rs.getString("callNumber"));
			holdRequest.setIssuedDate(SqlUtils.getDate(rs.getString("issuedDate")));
			holdRequests.add(holdRequest);
		}
		return holdRequests;
	}
	
	public void deleteHoldRequest(String hid) throws SQLException {
		Map<String,String> keyVals = Maps.newHashMap();
		keyVals.put("hid", hid);
		String sql = SqlUtils.deleteSql("HoldRequest", keyVals);
		PreparedStatement ps = connection.prepareStatement(sql);
		ps.executeUpdate();
	}
}
